<?php
/*
weibo 类
*/
class Com_weibo extends Acan{
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		//header('Content-Type: text/html; charset=UTF-8');
		$this->wbk = 'mobile';
		$this->wbkey = array(
			"web"=>array(
				"WB_AKEY"=>'3365462142',
				"WB_SKEY"=>'36b65464e2ffe206ee5fecc5e4500d4e',
			),
			"mobile"=>array(
				"WB_AKEY"=>'1480042123',
				"WB_SKEY"=>'722e13d205ab5bde460fe33efce3d193',
			)
		);
		define( "WB_CALLBACK_URL" , "http://life.nice9s.com:93/weibo-api-auth_call");
	}
	function code_url($display='page'){
		include_once( A_ROOT.'/plugin/weibo-phpsdk-v2/saetv2.ex.class.php' );
		$o = new SaeTOAuthV2( $this->wbkey[$this->wbk]['WB_AKEY'] , $this->wbkey[$this->wbk]['WB_SKEY'] );
		return $o->getAuthorizeURL( WB_CALLBACK_URL ).'&display='.$display;
	}
	function auth_check(){
		$c = new SaeTClientV2( $this->wbkey[$this->wbk]['WB_AKEY'] , $this->wbkey[$this->wbk]['WB_SKEY'] ,$_SESSION['token']['access_token']);
		return $c;
	}
	function auth($display='page'){
		$this->_S = Afun_session();
		if($display=='web') $this->wbk = 'web';
		//include_once( A_ROOT.'/plugin/weibo-phpsdk-v2/config.php' );
		include_once( A_ROOT.'/plugin/weibo-phpsdk-v2/saetv2.ex.class.php' );
		$o = new SaeTOAuthV2( $this->wbkey[$this->wbk]['WB_AKEY'] , $this->wbkey[$this->wbk]['WB_SKEY'] );
		if (isset($_REQUEST['code'])) {
			$keys = array();
			$keys['code'] = $_REQUEST['code'];
			$keys['redirect_uri'] = WB_CALLBACK_URL;
			try {
				$token = $o->getAccessToken( 'code', $keys ) ;
			} catch (OAuthException $e) {
			}
			if ($token) {
				$_SESSION['token'] = $token;
				setcookie( 'weibojs_'.$o->client_id, http_build_query($token) );
			}
		}
		if($display=='web'){
			if(!empty($_REQUEST["signed_request"])){
				$data=$o->parseSignedRequest($_REQUEST["signed_request"]);
				if($data=='-2'){
					 die('签名错误!');
				}else{
					$_SESSION['token'] = $data;
					if(empty($_SESSION['token']['user_id'])){
						return false;
					}
					if($_SESSION['token']['oauth_token']){
						$_SESSION['token']['access_token'] = $_SESSION['token']['oauth_token'];
					}else{
						return false;
					}
				}
			}else{
				return false;
			}
		}
		if(empty($_SESSION['token'])){
			return $o->getAuthorizeURL( WB_CALLBACK_URL ).'&display='.$display;
		}
		$login=false;
		//判断uid是否存在 或者 登入时间是否超过10分钟
//		if(!empty($this->_S['Auth']['oid']) && !empty($this->_S['Auth']['hash']) && (Afun_get_time()-$this->_S['Auth']['time'])<600){}
		if(!$login){
			$c = new SaeTClientV2( $this->wbkey[$this->wbk]['WB_AKEY'] , $this->wbkey[$this->wbk]['WB_SKEY'] ,$_SESSION['token']['access_token']);
			$getuser = $c->get_uid();
			if($getuser['error_code'] == 21301){
				unset($_SESSION['token']);
			}
			$oid = $getuser['uid'];
			$userinfo = $c->show_user_by_id($oid);
			$username = $userinfo['screen_name'];
			$this->_S['Auth']['oid'] = $oid;
			$fmodel = Afun_db()->model(Acan_table_fate);
			$fdata = $fmodel->find("oid=$oid");
			$fbj->oid = $oid;
			$enum_sex = array('m'=>1,'f'=>2,'n'=>3);
			$fbj->sex = $enum_sex[$userinfo['gender']];
			$fbj->oid = $oid;
			$sync = true;
			if(!$fdata){
				if($this->_S['Auth']['uid']>0){//登入uid,同步oid 
					$uid=$this->_S['Auth']['uid'];
					$udata = $fmodel->find("uid=$uid");
					if($udata){
						$fid = $udata[$fmodel->key_name];
						if($udata['oid']>0){
							$uid=0;
						}else{
							$fmodel->updatePk("oid=$oid",$fid);//如果fate存在uid,不存在oid 则更新fate的oid
							$sync = false;
						}
					}
				}
				if($sync){
					if($uid<1){
						$rs = Afunc_user()->add(array(),1,1);//生成一个密码随机的无用户名的uid
						$uid=$rs->uid;
					}
					$fbj->uid = $uid;
					$fmodel->save($fbj);
					$fid = $fmodel->lastInsertId();
				}
			}else{//存在fate
				$fid=$fdata[$fmodel->key_name];
				if($fdata['uid']<1){
					if($this->_S['Auth']['uid']>0){//存在uid
						$uid=$this->_S['Auth']['uid'];
						$udata = $fmodel->find("uid=$uid");
						if($udata){
							$fmodel->deletePk($fid);//删除oid记录
							$fid = $udata[$fmodel->key_name];
							$fmodel->updatePk("oid=$oid",$fid);//更新uid记录
							$sync = false;
						}
					}else{//不存在uid则添加
						$rs = Afunc_user()->add(array(),1,1);//生成一个密码随机的无用户名的uid
						$uid=$rs->uid;
					}
					if($sync){
						$fbj->uid = $uid;
						$fmodel->updatePk("uid=$uid",$fid);//存在oid不存在uid，则更新uid
					}
				}else{
					$uid=$fdata['uid'];
				}
			}
			$uinfo = Afunc_user()->finduid($uid);
			if(!empty($uinfo->mail)){
				$mail = $uinfo->mail;
			}
			if(!empty($uinfo->username)){
				$username = $uinfo->username;
			}else{
				$uinfo->username = $username;
				if(empty($uinfo->mail)){unset($uinfo->mail);}
				Afunc_user()->save($uinfo);
			}
			$this->_S['Auth']['name'] = $username;
			$this->_S['Auth']['mail'] = $mail;
			$this->_S['Auth']['fid'] = $fid;
			$this->_S['Auth']['uid'] = $uid;
			$this->_S['Auth']['weibo'] = 1;
			$this->_S['Auth']['oid'] = $oid;
			$this->_S['Auth']['time'] = Afun_get_time("time");
			$this->_S['Auth']['hash'] = Afun_auth_hash($uid,$this->_S['Auth']['time']);
			/*
			$user['oid'] = $uid;
			$user['info'] = $c->show_user_by_id($uid);//根据ID获取用户等基本信息
			$user['info']['id'] = $user['info']['id'];
			$user['lasttime'] = Afun_get_time('time');
			$user['ip'] = Afun_get_ip();
			$user['Ahash'] = $this->hash($user['oid'],$user['ip']);//获取通信hash
			unset($user['info']['status']);
			Afun_session($user);*/
			Afun_session($this->_S);
		}
		return false;
	}
	
	function getbirthday($str){
		$str = trim($str);
		if(strlen($str)==0){
			return 0;
		}
		$constellation = Afunc_enum()->data('constellation');
		$xingzuo = array_flip($constellation);
		if($xingzuo[$str]>0){
			$data['type'] = 'xingzuo';
			$data['val'] = $xingzuo[$str];
		}elseif(preg_match("/([0-9]{1,4})年([0-9]{1,2})月([0-9]{1,2})日/",$str,$mt)){
			$data['type'] = 'birthday';
			if(strlen($mt[2])==1){$mt[2]='0'.$mt[2];}
			if(strlen($mt[3])==1){$mt[3]='0'.$mt[3];}
			$data['val'] = $mt[1].'-'.$mt[2].'-'.$mt[3];
		}elseif(preg_match("/([0-9]{1,2})月([0-9]{1,2})日/",$str,$mt)){
			$data['type'] = 'birthday';
			if(strlen($mt[1])==1){$mt[1]='0'.$mt[1];}
			if(strlen($mt[2])==1){$mt[2]='0'.$mt[2];}
			$data['val'] = '1900-'.$mt[1].'-'.$mt[2];
		}
		return $data;
	}
	function sendmsg($toid,$note){
		include_once( A_ROOT.'/plugin/weibo-phpsdk-v2/saetv2.ex.class.php' );
		$c = new SaeTClientV2( $this->wbkey[$this->wbk]['WB_AKEY'] , $this->wbkey[$this->wbk]['WB_SKEY'] ,$_SESSION['token']['access_token']);
		$rs = $c->send_dm_by_id($toid,$note);
		$obj->chatid = $rs['id'];
		return $obj;
	}
	
	//站内应用
	function auth_zhannei(){
		include_once( A_ROOT.'/plugin/weibo-phpsdk-v2/saetv2.ex.class.php' );
		if (empty($_SESSION['oauth2']["user_id"])) {
			include "/plugin/weibo/auth.php";
			exit;
		} else {
			$login=false;
			//判断uid是否存在 或者 登入时间是否超过10分钟
			if(!empty($this->_S['oid']) && !empty($this->_S['Ahash']) && (Afun_get_time()-$this->_S['lasttime'])<600){
				$login=true;
			}
			if(!$login){
				$c = new SaeTClientV2( $this->wbkey[$this->wbk]['WB_AKEY'] , $this->wbkey[$this->wbk]['WB_SKEY'] ,$_SESSION['oauth2']['oauth_token'] ,'' );
				$getuser = $c->get_uid();
				$uid = $getuser['uid'];
				$user['oid'] = $uid;
				$user['info'] = $c->show_user_by_id($uid);//根据ID获取用户等基本信息
				$user['info']['id'] = $user['info']['id'];
				$user['lasttime'] = Afun_get_time('time');
				$user['ip'] = Afun_get_ip();
				$user['Ahash'] = Afun_new('weibo')->hash($user['oid'],$user['ip']);//获取通信hash
				unset($user['info']['status']);
				$this->_S = Afun_session($user);
			}
			//$ms  = $c->home_timeline(); // done
			//var_dump($c->followers_ids_by_id('1734317652'));
			//print_r($user_message);
		}
	}
	//加为好友
	function add_friend($oid){
		include_once( A_ROOT.'/plugin/weibo-phpsdk-v2/saetv2.ex.class.php' );
		$c = new SaeTClientV2( $this->wbkey[$this->wbk]['WB_AKEY'] , $this->wbkey[$this->wbk]['WB_SKEY'] ,$_SESSION['token']['access_token']);
		$rs = $c->follow_by_id($oid);
		$this->auth_error($rs);
		return $rs;
	}
	//用户相册
	function users_photos($oid){
		include_once( A_ROOT.'/plugin/weibo-phpsdk-v2/saetv2.ex.class.php' );
		$c = new SaeTClientV2( $this->wbkey[$this->wbk]['WB_AKEY'] , $this->wbkey[$this->wbk]['WB_SKEY'] ,$_SESSION['token']['access_token']);
		$rs = $c->place_users_photos($oid);
		$this->auth_error($rs);
		return $rs;
	}
	function auth_error($rs){
	
	}
	//生成验证
	function hash($uid,$ip){
		return hash('sha1',$uid.$ip.Acan_auth_key);
	}
	//判断性别
	function sex_decode($str){
		if($str=='m'){
			$code = 1;
		}elseif($str=='f'){
			$code = 2;
		}elseif($str=='n'){
			$code = 3;
		}
		return $code;
	}
}
?>